Import data from a csv
In [97]:
import pandas as pd
df = pd.read_csv('Zip_Zri_MultiFamilyResidenceRental.csv')
df.iloc[[0,1,3]]
Out[97]:
Fill NaN values with the a string of None
In [98]:
df_none=df.fillna(value='None')
df_none.iloc[[0,1,3]]
Out[98]:
Fill NaN values with specific numeric values
In [99]:
df_zero=df.fillna(0)
df_zero.iloc[[0,1,3]]
Out[99]:
Fill NaN values using forward values
In [100]:
df_forward_fill=df.fillna(method='ffill')
df_forward_fill.iloc[[0,1,3]]
Out[100]:
Let us inspect that a forward fill a little further
In [101]:
df.iloc[[3]][['2010-09','2010-10','2014-04','2014-05','2014-05','2014-06','2016-01','2019-12','2020-01']]
Out[101]:
In [102]:
df.iloc[[2,3,4]][['2010-09','2010-10','2014-04','2014-05','2014-05','2014-06','2016-01','2019-12','2020-01']]
Out[102]:
In [103]:
df_forward_fill.iloc[[3]][['2010-09','2010-10','2014-04','2014-05','2014-05','2014-06','2016-01']]
Out[103]:
In [104]:
df_forward_fill.iloc[[2,3,4]][['2010-09','2010-10','2014-04','2014-05','2014-05','2014-06','2016-01']]
Out[104]:
Using ffill for forward fill without passing the axis will forward fill by using the previous rows value for that column. Using the axis parameter we can get the next column value instead of the row value like so:
In [105]:
df_forward_fill_axis_one=df.fillna(method='ffill',axis=1)
df_forward_fill_axis_one.iloc[[2,3,4]][['SizeRank','2010-09','2010-10','2014-04','2014-05','2014-05','2014-06','2016-01']]
Out[105]:
Before the forward fill
In [106]:
df.iloc[[2,3,4]][['SizeRank','2010-09','2010-10','2014-04','2014-05','2014-05','2014-06','2016-01']]
Out[106]:
Now for this data set it is probably most appropriate to back fill using the future column values
In [107]:
df_back_fill_axis_one=df.fillna(method='bfill',axis=1)
df_back_fill_axis_one.iloc[[2,3,4]][['SizeRank','2010-09','2010-10','2010-11','2011-02','2014-03','2014-04']]
Out[107]: